<?php
class retrieve_standard_forms extends retrieve_base{

	private $module_display_options 	= array();
	/**
	* function produces abstract structure of modules db structure
	*
    * used to generate the table structure required abstract function that passes an abstract
	* representation of the desired table structure to the proper database module which will
	* interpet the abstract and convert it into a valid SQL Create table structure.
	*
	* @return Array list of abstract table definitions for this module
	*/
	function create_table(){
		$tables = array();
		/**
		* Table structure for table 'available_fields'
		*/
		
		$fields = array(
			array("af_identifier"	,"unsigned integer"			,"NOT NULL"	,"auto_increment"),
			array("af_client"		,"unsigned integer"			,"NOT NULL"	,"default '0'"),
			array("af_form"			,"unsigned integer"			,"NOT NULL"	,"default ''"),
			array("af_available"	,"unsigned small integer"	,"NOT NULL"	,"default ''"),
			array("af_name"			,"VARCHAR(255)"				,"NOT NULL"	,"default ''"),
			array("af_locale"		,"VARCHAR(255)"				,"NOT NULL"	,"default ''"),
			array("af_required"		,"unsigned small integer"	,"NOT NULL"	,"default ''")
		);

		$primary ="af_identifier";
		$tables[count($tables)] = array("available_fields", $fields, $primary);
		
		/**
		* Table structure for table 'available_forms'
		*/
		
		$fields = array(
			array("frm_identifier"		,"unsigned integer"		,"NOT NULL"	,"auto_increment"),
			array("frm_client"			,"unsigned integer"		,"NOT NULL"	,"default '0'"),
			array("frm_label"			,"VARCHAR(255)"			,"NOT NULL"	,"default ''"),
			array("frm_date_created"	,"datetime"				,"NOT NULL"	,"default ''"),
			array("frm_email_address"	,"VARCHAR(255)"			,"NOT NULL"	,"default ''")
		);

		$primary ="frm_identifier";
		$tables[count($tables)] = array("available_forms", $fields, $primary);
		/**
		* F O R M   B U I L D E R   T A B L E S
		-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
		* form_builder_data            - holds the initial record inserted by a user
		* form_builder_fields          - holds the field information for the form that was submitted
		* form_builder_group_fields    - holds the fields that are groupable
		* form_builder_emails          - holds the subject line(s) and the email address(s) to use.
		* form_builder_locations       - holds the locations that the form will be published to.
		* form_builder_required_fields - holds the fields that are required to be filled in non blank.
		* form_builder_structure       - holds the editable structure of the form. (main function)
		*/
		
		/**
		* form_builder_structure
		*/
		$fields = array(
			array("fb_identifier"			,"unsigned integer"			,"NOT NULL"	,"auto_increment"),
			array("fb_client"				,"unsigned integer"			,"NOT NULL"	,"default '0'"),
			array("fb_xml_structure"		,"text"						,"NOT NULL"	,"default ''"),
			array("fb_label"				,"VARCHAR(255)"				,"NOT NULL"	,"default ''"),
			array("fb_action"				,"unsigned small integer"	,"NOT NULL"	,"default ''"),
			array("fb_created"				,"datetime"					,"NOT NULL"	,"default ''"),
			array("fb_creator"				,"unsigned integer"			,"NOT NULL"	,"default ''"),
			array("fb_url"					,"varchar(255)"				,"NOT NULL"	,"default ''"),
			array("fb_method"				,"varchar(5)"				,"NOT NULL"	,"default 'post'"),
			array("fb_confirm_screen"		,"text"						,"NOT NULL"	,"default ''"),
			array("fb_emailscreen"			,"text"						,"NOT NULL"	,"default ''"),
			array("fb_submit_button_label"	,"varchar(255)"				,"NOT NULL"	,"default ''"),
			array("fb_default_email"		,"varchar(255)"				,"NULL"	,"default ''"),
			array("fb_default_subject"		,"varchar(255)"				,"NULL"	,"default ''"),
			array("fb_status"				,"unsigned small integer"	,"NOT NULL"	,"default '4'")
		);

		$primary ="fb_identifier";
		$tables[count($tables)] = array("form_builder_structure", $fields, $primary);

		/**
		* form_builder_required_fields
		*/
		$fields = array(
			array("fbr_identifier"		,"unsigned integer"			,"NOT NULL"	,"auto_increment"),
			array("fbr_client"			,"unsigned integer"			,"NOT NULL"	,"default '0'"),
			array("fbr_form"			,"unsigned integer"			,"NOT NULL"	,"default '0'"),
			array("fbr_index"			,"unsigned integer"			,"NOT NULL"	,"default '0'")
		);

		$primary ="fbr_identifier";
		$tables[count($tables)] = array("form_builder_required_fields", $fields, $primary);

		/**
		* form_builder_group_fields
		*/
		$fields = array(
			array("fbg_identifier"		,"unsigned integer"			,"NOT NULL"	,"auto_increment"),
			array("fbg_client"			,"unsigned integer"			,"NOT NULL"	,"default '0'"),
			array("fbg_form"			,"unsigned integer"			,"NOT NULL"	,"default '0'"),
			array("fbg_index"			,"unsigned integer"			,"NOT NULL"	,"default '0'")
		);

		$primary ="fbg_identifier";
		$tables[count($tables)] = array("form_builder_group_fields", $fields, $primary);
		/**
		* form_builder_locations
		*/
		$fields = array(
			array("fbl_identifier"		,"unsigned integer"			,"NOT NULL"	,"auto_increment"),
			array("fbl_client"			,"unsigned integer"			,"NOT NULL"	,"default '0'"),
			array("fbl_form"			,"unsigned integer"			,"NOT NULL"	,"default '0'"),
			array("fbl_location"		,"unsigned integer"			,"NOT NULL"	,"default '0'")
		);

		$primary ="fbl_identifier";
		$tables[count($tables)] = array("form_builder_locations", $fields, $primary);
		/**
		* form_builder_fields
		*/
		$fields = array(
			array("fbf_identifier"		,"unsigned integer"			,"NOT NULL"	,"auto_increment"),
			array("fbf_client"			,"unsigned integer"			,"NOT NULL"	,"default '0'"),
			array("fbf_field"			,"varchar(255)"				,"NOT NULL"	,"default '0'"),
			array("fbf_link"			,"unsigned integer"			,"NOT NULL"	,"default '0'"),
			array("fbf_value"			,"blob"						,"NOT NULL"	,"default ''"),
			array("fbf_value_group"		,"varchar(255)"				,"NOT NULL"	,"default ''"),
			array("fbf_mime_type"		,"varchar(255)"				,"NOT NULL"	,"default ''")
		);

		$primary ="fbf_identifier";
		$tables[count($tables)] = array("form_builder_fields", $fields, $primary);
		/**
		* form_builder_emails
		*/
		$fields = array(
			array("fbe_identifier"		,"unsigned integer"			,"NOT NULL"	,"auto_increment"),
			array("fbe_client"			,"unsigned integer"			,"NOT NULL"	,"default '0'"),
			array("fbe_form"			,"unsigned integer"			,"NOT NULL"	,"default '0'"),
			array("fbe_default"			,"unsigned small integer"	,"NOT NULL"	,"default '0'"),
			array("fbe_email"			,"varchar(255)"				,"NOT NULL"	,"default ''"),
			array("fbe_subject"			,"varchar(255)"				,"NOT NULL"	,"default ''")
		);

		$primary ="fbe_identifier";
		$tables[count($tables)] = array("form_builder_emails", $fields, $primary);
		/**
		* form_builder_emails
		*/
		$fields = array(
			array("fbd_identifier"		,"unsigned integer"			,"NOT NULL"	,"auto_increment"),
			array("fbd_client"			,"unsigned integer"			,"NOT NULL"	,"default '0'"),
			array("fbd_form"			,"unsigned integer"			,"NOT NULL"	,"default '0'"),
			array("fbd_filled_in_by"	,"unsigned integer"			,"NOT NULL"	,"default '0'"),
			array("fbd_submitted"		,"datetime"					,"NOT NULL"	,"default ''")
		);

		$primary ="fbd_identifier";
		$tables[count($tables)] = array("form_builder_data", $fields, $primary);
		/**
		* form_builder_field_types
		*/
		$fields = array(
			array("fbft_identifier"		,"unsigned integer"			,"NOT NULL"	,"auto_increment"),
			array("fbft_client"			,"unsigned integer"			,"NOT NULL"	,"default '0'"),
			array("fbft_form"			,"unsigned integer"			,"NOT NULL"	,"default '0'"),
			array("fbft_field"			,"varchar(255)"				,"NOT NULL"	,"default ''"),
			array("fbft_type"			,"varchar(255)"				,"NOT NULL"	,"default ''")
		);

		$primary ="fbft_identifier";
		$tables[count($tables)] = array("form_builder_field_types", $fields, $primary);

		return $tables;
	}

	

	/**
	* call the initialisation function only when this module is created
	*/
	function create_client_details($parameters){
		$client_identifier = $this->check_parameters($parameters,"client_identifier",-1);
		$email_address = $this->check_parameters($parameters,"email_address");
		/**
		* insert available_fields data
		*/
		$data = array();
		$counter=0;
		$length=count($this->parent->modules);
		foreach($this->parent->modules as $index => $moduleEntry){
			if($this->parent->modules[$index]["cc_code"]==1){
			//print "<li>looking for :".$this->parent->modules[$index]["tag"]."LIST_FORM_FIELD_ACCESS";
				$tables = $this->call_command($this->parent->modules[$index]["tag"]."LIST_FORM_FIELD_ACCESS");
				if (is_array($tables)){
					$m = count($tables);
					for ($i=0;$i<$m;$i++){
						if (!empty($tables[$i])){
							$data[$counter++] = $tables[$i];
						}
					}
				}
			}
		}
		$length_array = count($data);
		for ($index=0; $length_array > $index;$index++){
			$sql = "INSERT INTO available_forms (frm_client, frm_label, frm_email_address) VALUES ($client_identifier,'".$data[$index][0]."','$email_address');";
//			print "<li>$sql</li>";
			$this->db->query($sql);
			$result = $this->db->query("select * from available_forms  where frm_client=$client_identifier and frm_label='".$data[$index][0]."';");
			$id = "";
			while ($r = $this->db->fetch_array($result)){
				$id = $r["frm_identifier"];
			}
			for ($i=0,$len=count($data[$index][1]);$i<$len;$i++){
				eval ($data[$index][1][$i]);
//				print "<li>$sql</li>";
				$this->db->query($sql);
			}
		}
		
	}
		

}
?>